package com.ruoyi.cderp.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.cderp.domain.TblProductStock;
import com.ruoyi.cderp.mapper.TblProductStockMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.TblStockCostAdjustSheetDetail;
import com.ruoyi.cderp.mapper.TblStockCostAdjustSheetMapper;
import com.ruoyi.cderp.domain.TblStockCostAdjustSheet;
import com.ruoyi.cderp.service.ITblStockCostAdjustSheetService;

/**
 * 库存成本调整单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-06
 */
@Service
public class TblStockCostAdjustSheetServiceImpl implements ITblStockCostAdjustSheetService
{
    @Autowired
    private TblStockCostAdjustSheetMapper tblStockCostAdjustSheetMapper;
    @Autowired
    private TblProductStockMapper tblProductStockMapper;

    /**
     * 查询库存成本调整单
     *
     * @param id 库存成本调整单主键
     * @return 库存成本调整单
     */
    @Override
    public TblStockCostAdjustSheet selectTblStockCostAdjustSheetById(String id)
    {
        return tblStockCostAdjustSheetMapper.selectTblStockCostAdjustSheetById(id);
    }

    /**
     * 查询库存成本调整单列表
     *
     * @param tblStockCostAdjustSheet 库存成本调整单
     * @return 库存成本调整单
     */
    @Override
    public List<TblStockCostAdjustSheet> selectTblStockCostAdjustSheetList(TblStockCostAdjustSheet tblStockCostAdjustSheet)
    {
        return tblStockCostAdjustSheetMapper.selectTblStockCostAdjustSheetList(tblStockCostAdjustSheet);
    }

    /**
     * 新增库存成本调整单
     *
     * @param tblStockCostAdjustSheet 库存成本调整单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblStockCostAdjustSheet(TblStockCostAdjustSheet tblStockCostAdjustSheet)
    {
        tblStockCostAdjustSheet.setCreateTime(DateUtils.getNowDate());
        int rows = tblStockCostAdjustSheetMapper.insertTblStockCostAdjustSheet(tblStockCostAdjustSheet);
        insertTblStockCostAdjustSheetDetail(tblStockCostAdjustSheet);
        return rows;
    }

    /**
     * 修改库存成本调整单
     *
     * @param tblStockCostAdjustSheet 库存成本调整单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblStockCostAdjustSheet(TblStockCostAdjustSheet tblStockCostAdjustSheet)
    {
        for (int i = 0; i < tblStockCostAdjustSheet.getTblStockCostAdjustSheetDetailList().size(); i++) {
            TblStockCostAdjustSheetDetail tblStockCostAdjustSheetDetail = tblStockCostAdjustSheet.getTblStockCostAdjustSheetDetailList().get(i);
            if (tblStockCostAdjustSheetDetail.getOriPrice() != tblStockCostAdjustSheetDetail.getPrice()){
                TblProductStock t1 = new TblProductStock();
                t1.setScId(tblStockCostAdjustSheet.getScId());
                t1.setProductId(tblStockCostAdjustSheetDetail.getProductId());

                TblProductStock t2 = tblProductStockMapper.selectTblProductStockByScIdAndProductId(t1);
                t2.setTaxPrice(tblStockCostAdjustSheetDetail.getPrice());
                t2.setTaxAmount(tblStockCostAdjustSheetDetail.getPrice().multiply(new BigDecimal(t2.getStockNum())));

                tblProductStockMapper.updateTblProductStock(t2);
            }
        }
        tblStockCostAdjustSheet.setUpdateTime(DateUtils.getNowDate());
        tblStockCostAdjustSheetMapper.deleteTblStockCostAdjustSheetDetailBySheetId(tblStockCostAdjustSheet.getId());
        insertTblStockCostAdjustSheetDetail(tblStockCostAdjustSheet);
        return tblStockCostAdjustSheetMapper.updateTblStockCostAdjustSheet(tblStockCostAdjustSheet);
    }

    /**
     * 批量删除库存成本调整单
     *
     * @param ids 需要删除的库存成本调整单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblStockCostAdjustSheetByIds(String[] ids)
    {
        tblStockCostAdjustSheetMapper.deleteTblStockCostAdjustSheetDetailBySheetIds(ids);
        return tblStockCostAdjustSheetMapper.deleteTblStockCostAdjustSheetByIds(ids);
    }

    /**
     * 删除库存成本调整单信息
     *
     * @param id 库存成本调整单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblStockCostAdjustSheetById(String id)
    {
        tblStockCostAdjustSheetMapper.deleteTblStockCostAdjustSheetDetailBySheetId(id);
        return tblStockCostAdjustSheetMapper.deleteTblStockCostAdjustSheetById(id);
    }

    /**
     * 新增库存成本调整单明细信息
     *
     * @param tblStockCostAdjustSheet 库存成本调整单对象
     */
    public void insertTblStockCostAdjustSheetDetail(TblStockCostAdjustSheet tblStockCostAdjustSheet)
    {
        List<TblStockCostAdjustSheetDetail> tblStockCostAdjustSheetDetailList = tblStockCostAdjustSheet.getTblStockCostAdjustSheetDetailList();
        String id = tblStockCostAdjustSheet.getId();
        if (StringUtils.isNotNull(tblStockCostAdjustSheetDetailList))
        {
            List<TblStockCostAdjustSheetDetail> list = new ArrayList<TblStockCostAdjustSheetDetail>();
            for (TblStockCostAdjustSheetDetail tblStockCostAdjustSheetDetail : tblStockCostAdjustSheetDetailList)
            {
                tblStockCostAdjustSheetDetail.setSheetId(id);
                list.add(tblStockCostAdjustSheetDetail);
            }
            if (list.size() > 0)
            {
                tblStockCostAdjustSheetMapper.batchTblStockCostAdjustSheetDetail(list);
            }
        }
    }
}
